<template>
  <div>
    <Form
      oneitem="商品类型搜索："
      :select-show="false"
      :total-count="totalCount"
      :total-page="totalPage"
      :search-list="skuList"
      @searchResultFn="searchResultFn"
      @newCreateFn="newCreateFn"
    >
      <template #tableForm>
        <el-table-column label="序号" width="80" type="index" :index="indexMethod" />
        <el-table-column prop="className" label="商品类型名称" />
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button type="text" @click="edit(scope.row)">修改</el-button>
            <el-button type="text" style="color:red" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </template>
    </Form>
    <el-dialog
      :title="skuForm.classId?'修改商品类型':'新增商品类型'"
      :visible.sync="skuVisible"
      width="630px"
      @closed="closeFn"
    >
      <el-form ref="skuForm" :model="skuForm" :rules="rules" label-width="100px">
        <el-form-item label="活动名称" prop="className">
          <el-input
            v-model="skuForm.className"
            maxlength="10"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button id="buttom2" @click="skuVisible=false">取 消</el-button>
        <el-button id="buttom1" type="primary" @click="creatFn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Form from '@/components/form/index.vue'
import { skuClass, creatSku, editSku, delSku } from '@/api'
export default {
  components: { Form },
  data() {
    return {
      totalCount: null,
      totalPage: null,
      pageIndex: 1,
      skuList: [],
      skuVisible: false,
      skuForm: {
        className: null
      },
      rules: {
        className: [{ required: true, message: '请输入' }]
      }
    }
  },
  async  created() {
    await this.skuFn(this.pageIndex)
  },
  methods: {
    indexMethod(index) {
      return this.pageIndex === 1 ? index + 1 : Number(`${this.pageIndex - 1}${index}`) + 1
    },
    // 列表
    async skuFn(pageIndex, className) {
      const res = await skuClass({
        pageIndex,
        pageSize: 10,
        className
      })
      this.skuList = res.data.currentPageRecords
      this.totalCount = res.data.totalCount
      this.totalPage = res.data.totalPage
      return res.data
    },
    // 搜索
    async searchResultFn(val) {
      await this.skuFn(1, val.taskCode)
    },
    // 新建
    newCreateFn() {
      this.skuVisible = true
    },
    async edit(row) {
      this.skuVisible = true
      const res = await this.skuFn(1, row.className)
      console.log(res)
      this.skuForm = res.currentPageRecords[0]
    },
    closeFn() {
      this.skuForm = {
        className: null
      }
      this.$refs.skuForm.resetFields()
    },
    async creatFn() {
      this.$refs.skuForm.validate(async(valid) => {
        if (!valid) return
        if (this.skuForm.classId) {
          await editSku(this.skuForm.classId, this.skuForm).then(() => {
            this.$message({
              type: 'success',
              message: '修改成功'
            })
          }).catch(res => {
            this.$message({
              type: 'warning',
              message: res.response.data
            })
          })
        } else {
          await creatSku(this.skuForm).then(() => {
            this.$message({
              type: 'success',
              message: '添加成功'
            })
          }).catch(res => {
            this.$message({
              type: 'warning',
              message: res.response.data
            })
          })
        }
      })
      await this.skuFn(this.pageIndex)
      this.skuVisible = false
    },
    del(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delSku(row.classId)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        await this.skuFn(this.pageIndex)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style>

</style>
